package com.hok.misc.ftp;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.net.ftp.FTPClient;

public class Test {
	static int test;
	// static Properties properties;
	// static {
	// properties = new Properties();
	// try {
	// System.out.println(Test.class
	// .getResourceAsStream("softwarePackage.properties"));
	// properties.load(Test.class
	// .getResourceAsStream("softwarePackage.properties"));
	// System.err.println("==================================");
	// System.err.println(properties.getProperty("local"));
	// System.err.println(properties.getProperty("host"));
	// } catch (IOException e) {
	// e.printStackTrace();
	// }
	// }
	static Properties properties;
	static String local;
	static String host;
	static String username;
	static String password;
	static String sourceDir;
	static String shellName;
	static {
		properties = new Properties();
		try {
			properties.load(Test.class
					.getResourceAsStream("softwarePackage.properties"));
			local = (String) properties.get("local");
			host = (String) properties.get("host");
			username = (String) properties.get("username");
			password = (String) properties.get("password");
			sourceDir = (String) properties.get("source.dir");
			shellName = (String) properties.get("shellName");
		} catch (IOException e) {
			e.printStackTrace();
			
		}
	}

	private String test() {
		return "";
		
	}
	private Integer test(Integer i) {
		return i;
		
	}
	public static void main(String[] args) throws IOException {
		String te = "12,34,43,45,";
		System.out.println(te.substring(1,te.length() -1));
		System.out.println(test);
		Map<String, String> map = System.getenv();
		Set<String> set = map.keySet();
		for(String key : set) {
			System.out.println(key + ": " + map.get(key));
		}
		
		System.err.println("=============================");
		Properties props = System.getProperties();
		Set<Object> keys = props.keySet();
		for(Object o : keys) {
			System.out.println(o.toString() + "\t" + props.get(o));
		}
		File f = new File("D:\\workspace\\dframe\\dbm_gwt\\war\\WEB-INF\\lib\\jef.properties");
		System.out.println(f.getParent());
		String ss = "abcdef\n";
		System.out.println(ss.substring(0, ss.length()-2));
		System.out.println("hello");
		//connectDB5();
		System.out.println(Calendar.getInstance().getTimeInMillis());
		System.err.println(MyBean.class);
		
		
		Date start = new Date();
		start.setTime(start.getTime() - 24 * 3600 * 1000);
		System.err.println(start);
		
		System.out.println(Test.class.getCanonicalName());
		/*String [] countries = Locale.getISOCountries();
	    String [] languages = Locale.getISOLanguages();
	    int i;
\
	    System.out.println("\nCountries:\n");
	    for (i = 0; i < countries.length; i++)
	      System.out.println(countries[i]);
	    System.out.println("\nLanguages:\n");
	    for (i = 0; i < languages.length; i++)
	      System.out.println(languages[i]);
	    Locale[] ls = Locale.getAvailableLocales();
	    for (int j = 0; j < ls.length; j++) {
			System.out.println(ls[j].getLanguage()+ "-" + ls[j].getCountry() + "\t" + ls[j].getDisplayLanguage()+ "\t" + ls[j].getCountry()+ "\t" + ls[j].getDisplayCountry() + "\t" +ls[j].getDisplayName());
		}
		System.out.println(Long.MIN_VALUE);//922 3372 0368 5477 5807
		System.out.println(System.getProperty("user.dir"));
		Random rd=new Random();
		System.out.println(File.separator+rd.nextInt(99999));
		System.out.println(InetAddress.getByName("news.sina.com.cn"));
		//Pattern pattern = Pattern.compile("\\(\\d{3}\\)\\s\\d{3}-\\d{4}");
		Pattern pattern = Pattern.compile("[\\w+[,]*\\w+]+");
		//Pattern pattern = Pattern.compile("\\w+[,]*\\w+");
		//Pattern pattern = Pattern.compile("[\\w+[,]*\\w+]+");
		Matcher matcher = pattern.matcher("hello1,hello2,hello3,hello4");
		
		while ( matcher.find() ) {
//	        System.out.println(match.group(0));
	        System.out.println(matcher.group(0));
	    }

		Pattern p2 = Pattern.compile("[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\\.?");
		Matcher m2 = p2.matcher("h-ed.sdsf.com.cn");
		while ( m2.find() ) {
	        System.out.println(m2.group(0));
	    }*/
		
		/*System.out.println(System.getenv("ailk"));
		System.out.println(System.getenv("hello"));
		String name = null;
		String age = null;
		String email = "hello";
		String fileName = "tnsnames.ora";
		Properties properties = new Properties();
		properties.load(Test.class.getResourceAsStream(fileName));
		Set<Object> keys = properties.keySet();
		for(Object key : keys) {
			System.out.println(key + " = " + properties.getProperty("key"));
		}*/
		
		
	/*	Map<String, String> map = new HashMap<String, String>();
		map.put(name + age + email, "ok");
		System.out.println(map.get(name + age + email));
		
		listFileFolder();

		List<String> idStrList = new ArrayList<String>();
		
		List<String> idList = new ArrayList<String>();
		for (int i = 0; i < 19; i++) {
			idList.add(String.valueOf(i));
		}
		
		int size = idList.size();
		
		int count = size/20 + 1;
		
	
		for (int i = 0; i < count; i++) {
			List<String> subList = null;
			int fromIndex = i * 5;
			if(count == 1) {
				subList = idList.subList(0, size);
			}
			
			if(i == count - 1) {
				subList = idList.subList(fromIndex, size);
			} else  {
				subList = idList.subList(fromIndex, fromIndex + 5);
			}
			
			StringBuffer ids = new StringBuffer();
			for (int j = 0; j < subList.size(); j++) {
				if (j == subList.size() - 1) {
					ids.append(subList.get(j)); 
				} else {
					ids.append(subList.get(j)).append(","); 
				}
			}
			idStrList.add(ids.toString());
		}
		
		for (int i = 0; i < idStrList.size(); i++) {
			System.out.println("-----------------");
			System.out.println(idStrList.get(i));
		}*/
//		
		
		
		
//		 Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
//		 System.out.println(cal.get(Calendar.HOUR_OF_DAY));
//	      System.out.println(cal.getTime());
//		Calendar cc = Calendar.getInstance();
//		System.out.println(cc.getTime());
//		for(String id : TimeZone.getAvailableIDs()) {
//			cc.setTimeZone(TimeZone.getTimeZone(id));
//			System.out.println(id + "  " + cc.get(Calendar.HOUR_OF_DAY));
//		}
//		
		//testConnection();
		// String sql =
		// "SELECT ERR_CODE,REFINE_LIMIT,REFINE_ORDER,ERR_DESC,ERR_FILE_TYPE,ERR_OUT_INTERVAL FROM bs_err_code_define where 1=1";
		// String countSql = sql.toString();
		// countSql = countSql.substring(countSql.indexOf("FROM"),
		// countSql.length());
		// System.out.println(countSql);
		//System.out.println("====================connectDB5=========================");
		//connectDB5();
		//String ss = "abcd123456";
		//System.out.println(ss.substring(0, 7));
		//System.out.println(Test.class.getName());
		/*
		 * try { CommandExecutor.execute("10.10.12.11", "billing", "billing",
		 * ""); } catch (JSchException e) { e.printStackTrace(); } catch
		 * (IOException e) { e.printStackTrace(); }
		 */
		// t1();
		// try {
		// FTPClient ftpClient = getFTPClient("10.10.12.11", "billings",
		// "billing");
		// } catch (IOException e) {
		// e.printStackTrace();
		// }

		// System.out.println(getFormatSize(333322));
	}

	public static List<FileInfo> listFileFolder() {
		String path = (String) properties.get("source.dir");
		File file = new File(System.getProperty("user.home") + File.separator + path);
		List<FileInfo> fileInfoList = new ArrayList<FileInfo>();
		if(file == null) {
			return fileInfoList; 
		}
		File[] files = file.listFiles();
		for (File f : files) {
			FileInfo fileInfo = new FileInfo();
			fileInfo.setName(f.getName());
			fileInfo.setPath(f.getPath());
			if(f.isDirectory()) {
				List<FileInfo> fileInfos = new ArrayList<FileInfo>();
				File[] files_ = f.listFiles();
				for (File f_ : files_) {
					FileInfo fi = new FileInfo();
					fi.setName(f_.getName());
					fi.setPath(f_.getPath());
					System.out.println(f_.getName());
					fileInfos.add(fi);
				}
				fileInfo.setChildren(fileInfos);
			}
			System.out.println(f.getName());
		//	System.out.println(f.getPath());
			fileInfoList.add(fileInfo);
		}
		return fileInfoList;
	}
	
	public static void dealDBdelete1000(List<String> idList) {
		int size = idList.size();
		int count= size/1000 + 1;
		final List<String> idStrList = new ArrayList<String>();
		
		for (int i = 0; i < count; i++) {
			List<String> subList = null;
			int fromIndex = i * 1000;
			if(count == 1) {
				subList = idList.subList(0, size);
				break;
			}
			
			if(i == count - 1) {
				subList = idList.subList(fromIndex, size);
			} else  {
				subList = idList.subList(fromIndex, fromIndex + 1000);
			}
			
			StringBuffer ids = new StringBuffer();
			for (int j = 0; j < subList.size(); j++) {
				if (j == subList.size() - 1) {
					ids.append(subList.get(j)); 
				} else {
					ids.append(subList.get(j)).append(","); 
				}
			}
			idStrList.add(ids.toString());
		}
	}
	public static void testConnection() {
		try {
			String getURL = "http://10.10.10.180:8085/casemg/operate/drsee/drsee.jsp?1=1&host=10.10.10.180&user=jsvnpt&passwd=jsvnpt&path=";//URLEncoder.encode("http://www.baidu.com", "utf-8");
			URL getUrl = new URL(getURL);
			// ���ƴ�յ�URL�������ӣ�URL.openConnection�������URL�����ͣ�
			// ���ز�ͬ��URLConnection����Ķ�������URL��һ��http�����ʵ�ʷ�
			// �ص���HttpURLConnection
			HttpURLConnection connection = (HttpURLConnection) getUrl
					.openConnection();
			// �������ӣ�����ʵ����get requestҪ����һ���connection.getInputStream()
			// �����вŻ����������

			connection.connect();

			// ȡ������������ʹ��Reader��ȡ
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					connection.getInputStream(), "utf-8"));
			System.out.println("=============================");
			System.out.println("Contents of get request");
			System.out.println("=============================");
			String lines;
			while ((lines = reader.readLine()) != null) {
				System.out.println(lines);
			}
			reader.close();
			// �Ͽ�����
			connection.disconnect();
			System.out.println("=============================");
			System.out.println("Contents of get request ends");
			System.out.println("=============================");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void testDate() {
		Date date = new Date();
		System.out.println(date.getYear());
		System.out.println(date.getMonth());

		Calendar now = Calendar.getInstance();
		System.out.println(now.get(Calendar.YEAR));
		System.out.println(now.get(Calendar.MONTH));
		String startMonth = "" + now.get(Calendar.YEAR)
				+ (now.get(Calendar.MONTH) + 1);
		System.out.println(startMonth);
	}

	public static FTPClient getFTPClient(String host, String userName,
			String password) throws IOException {
		FTPClient ftpClient = new FTPClient();
		ftpClient.connect(host);
		boolean isLogin = ftpClient.login(userName, password);
		System.out.println(isLogin);
		ftpClient.setBufferSize(1024);
		ftpClient.setControlEncoding("UTF-8");
		ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
		return ftpClient;
	}

	public static void t1() {
		String s = System.getProperty("user.dir");
		Properties properties = System.getProperties();
		Collection<Object> values = properties.values();
		for (Object obj : values) {
			System.out.println(obj);
		}
		Enumeration<?> keys = properties.keys();
		while (keys.hasMoreElements()) {
			Object object = (Object) keys.nextElement();
			System.out.println(object + "="
					+ properties.getProperty(object.toString()));
		}
	}

	public static String getFormatSize(long size) {
		BigDecimal result = new BigDecimal(3.4565);
		System.out.println(result);
		result.setScale(2, BigDecimal.ROUND_HALF_UP);
		System.err.println(result);
		BigDecimal result1 = new BigDecimal((double) size / 1024);
		result1.setScale(2, BigDecimal.ROUND_HALF_UP);
		BigDecimal result2 = new BigDecimal((double) size / 1024 / 1024);
		result2.setScale(2, BigDecimal.ROUND_HALF_UP);
		BigDecimal result3 = new BigDecimal((double) size / 1024 / 1024 / 1024);
		result3.setScale(2, BigDecimal.ROUND_HALF_UP);
		BigDecimal result4 = new BigDecimal((double) size / 1024 / 1024 / 1024
				/ 1024);
		result4.setScale(2, BigDecimal.ROUND_HALF_UP);
		System.out.println((double) size / 1024);
		System.out.println(result1);
		System.out.println(result1.toPlainString());
		if (result1.compareTo(BigDecimal.ONE) < 0) {
			return size + " bytes";
		} else if (result2.compareTo(BigDecimal.ONE) < 0) {
			return result1.toString() + "K";
		} else if (result3.compareTo(BigDecimal.ONE) < 0) {
			return result2.toString() + "M";
		} else if (result4.compareTo(BigDecimal.ONE) < 0) {
			return result3.toString() + "G";
		} else {
			return result4.toString() + "P";
		}
	}

	public static void connectDB5() {
		Statement st = null;// ws_sus_keyword
		PreparedStatement pst = null;
		Connection conn = null;
		ResultSet rs = null;
		try {
			// jdbc:oracle:thin:@//10.1.252.121:1521/service_name
			//String url = "jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 10.1.252.121)(PORT = 1521)) (ADDRESS = (PROTOCOL = TCP)(HOST = 10.1.252.122)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED)(SERVICE_NAME=rac)))";
			 String url2 = "jdbc:oracle:thin:@//192.168.5.150:1521/thdb";//jdbc:oracle:thin:@//10.10.10.120:1521/ora1204
			// "test1","test1";
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn = DriverManager.getConnection(url2, "demo", "demo");
			st = conn.createStatement();
			// pst =
			// conn.prepareStatement("insert into ws_sus_keyword values(?,?,?,?,?,?,?,?,?,?)");
			rs = st.executeQuery("select sysdate from dual");
			//select count(*) from(select d.domain_Id, d.domain_Name, d.memo, f1.tag_Value, f2.tag_Value from Bm_Domain d, Bm_Form_Tag f1, Bm_Form_Tag f2 where d.domain_Name=f1.tag_id(+) and d.memo=f2.tag_id(+) and (f1.tag_version ='cn' or f1.tag_value is null) and (f2.tag_version ='cn' or f2.tag_value is null) order by d.domain_Id desc)
			//rs = st.executeQuery("select count(*) from(select d.domain_Id, d.domain_Name, d.memo, f1.tag_Value, f2.tag_Value from Bm_Domain d, Bm_Form_Tag f1, Bm_Form_Tag f2 where d.domain_Name=f1.tag_id(+) and d.memo=f2.tag_id(+) and (f1.tag_version ='cn' or f1.tag_value is null) and (f2.tag_version ='cn' or f2.tag_value is null) order by d.domain_Id desc)");
			if (rs.next()) {
				System.out.println("===================");
				System.err.println(rs.getString(1));
				System.out.println("===================");
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				st.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
	}
	class MyBean {
		
	}

}

